<title>获取业务数据下拉、单选、多选函数</title>
<!-- 
1、 	通过自定义sql查询构造下拉、单选、多选函数时，必须要用name和value作为字段别名,
	如部门下拉自定义sql,自定义sql编号命名为query_org:
	select id value,org_name name from sys_org
	
2、	查询的结果集如下：

	[
	    {
	        "name": "人事部",
	        "value": "40288ae767ee75f70167ee7bae0a0001"
	    },
	    {
	        "name": "财务部",
	        "value": "40288ae767ee75f70167ee7c06ba0002"
	    },
	    {
	        "name": "组织机构",
	        "value": "sys"
	    }
	]

3、	则调用如下函数即可：
#--
	<div class="layui-row layui-col-space1 task-row">
		#@colStart("部门",12)
			//选中值、必填、禁用选择
		 	#set(value="sys",required="required",disabled="disabled")
			#@getSelectBySql('query_org','org_id','请选择部门')
		#@colEnd()
        #@colStart("部门",12)
			#@getCheckboxBySql('query_org','org_id','请选择部门')
		#@colEnd()
        #@colStart("部门",12)
			#@getRadioBySql('query_org','org_id','请选择部门')
		#@colEnd()
	</div>
--#
 -->

<!-- 业务字典下拉函数 ，code:字典编号，name:元素name属性,text:选项名称 -->
#define getSelect(code,name,text)
<select id="select_#(code)_#(name==null?'':name.replace('.','_'))" name="#(name)" class="layui-input" lay-verType='tips' lay-filter="#(name)" lay-verify="#(required)" #if(!disabled)lay-search#end #(disabled)>
	<option value="">#(text)</option>
</select>
<script type="text/javascript">
	$(function(){
		getOption('#(code)','select',"#(value)","#(name)",false,"#(disabled)");
	});
</script>
### 清空值
#set(value="",disabled="",required="")	
#end

<!-- 自定义SQL下拉函数，code:sql编号，name:元素name属性,text:选项名称 -->
#define getSelectBySql(code,name,text)
<select id="select_#(code)_#(name==null?'':name.replace('.','_'))" name="#(name)" class="layui-input" lay-verType='tips' lay-filter="#(name)" lay-verify="#(required)" #if(!disabled)lay-search#end #(disabled)>
	<option value="">#(text)</option>
</select>
<script type="text/javascript">
	$(function(){
		getOption('#(code)','select',"#(value)","#(name)",true,"#(disabled)");
	});
</script>
#set(value="",disabled="",required="")	
#end

<!-- 业务字典单选函数 ，code:字典编号，name:元素name属性,text:选项名称-->
#define getRadio(code,name,text)
<div id="radio_#(code)_#(name==null?'':name.replace('.','_'))"></div>
<script type="text/javascript">
	$(function(){
		getOption("#(code)","radio","#(value)","#(name)",false,"#(disabled)");
	});
</script>
#set(value="",disabled="")	
#end
<!-- 自定义SQL单选函数，code:sql编号，name:元素name属性,text:选项名称 -->
#define getRadioBySql(code,name,text)
<div id="radio_#(code)_#(name==null?'':name.replace('.','_'))"></div>
<script type="text/javascript">
	$(function(){
		getOption("#(code)","radio","#(value)","#(name)",true,"#(disabled)");
	});
</script>
#set(value="",disabled="")	
#end

<!-- 业务字典多选函数，code:字典编号，name:元素name属性,text:选项名称 -->
#define getCheckbox(code,name,text)
<div id="checkbox_#(code)_#(name==null?'':name.replace('.','_'))"></div>
<script type="text/javascript">
	$(function(){
		getOption("#(code)","checkbox","#(value)","#(name)",false,"#(disabled)");
	});
</script>
#set(value="",disabled="")	
#end

<!-- 自定义SQL多选函数，code:sql编号，name:元素name属性,text:选项名称 -->
#define getCheckboxBySql(code,name,text)
<div id="checkbox_#(code)_#(name==null?'':name.replace('.','_'))"></div>
<script type="text/javascript">
	$(function(){
		getOption("#(code)","checkbox","#(value)","#(name)",true,"#(disabled)");
	});
</script>
#set(value="",disabled="")	
#end

#define getOption()
<script src="#(path)/static/libs/layui/layui.js?t=#now()" type="text/javascript" ></script>
<script type="text/javascript">
	
	//获取业务字典数据 
	function getOption(code,type,value,name,isSql,disabled){		
		var url='#(path)/portal/core/dictionary/value/getOption';
		if(isSql){
			url='#(path)/portal/form/sql/getOption';
		}
		var checkboxValue=value.split(',');
		$.ajax({
			url:url,
			data:{
				"code":code
			},
			type:"post",
			success:function(data){
				var option="";
				for(var i=0;i<data.length;i++){
					if(type=="radio"){
						if(data[i].value==value){
							option+='<input type="radio" name="'+name+'" value="'+data[i].value+'" title="'+data[i].name+'" checked="checked"'+disabled+'/>';
						}else{
							option+='<input type="radio" name="'+name+'" value="'+data[i].value+'" title="'+data[i].name+'" '+disabled+'/>';
						}
					}else if(type=="checkbox"){
						if(checkboxValue.length>0){
							var isCheck=false;
							$.each(checkboxValue,function(key,val){
								if(data[i].value==val){
									isCheck=true;
								}								
							});	
							
							if(isCheck){
								option+='<input type="checkbox" name="'+name+'" value="'+data[i].value+'" title="'+data[i].name+'" checked="checked"'+disabled+'/>';
							}else{
								option+='<input type="checkbox" name="'+name+'" value="'+data[i].value+'" title="'+data[i].name+'"'+disabled+' />';
							}				
						}else{
							option+='<input type="checkbox" name="'+name+'" value="'+data[i].value+'" title="'+data[i].name+'"'+disabled+' />';
						}
					}else{
						if(data[i].value==value){
						    option+='<option value="'+data[i].value+'" selected="selected">'+data[i].name+'</option>';
						}else{
							option+='<option value="'+data[i].value+'">'+data[i].name+'</option>';
						}
					}	
				}					
				name=name.replace(/\./g,"_");
				$('#'+type+"_"+code+"_"+name).append(option);
				//立即渲染元素，防止有些情况渲染不成功
				layui.use('form', function() {
					var form = layui.form;
					form.render();
				});
			}			
		});
	}

  </script>
#end